var express = require('express');
const multer=require('multer');

//console.log(date.toLocaleDateString( )+'');
var upload=multer({dest:'./upload/' });

router = express.Router(),
    fs = require('fs'),
    TITLE = '上传照片',
    AVATAR_UPLOAD_FOLDER = '/avatar/'
/* GET home page. */
router.get('/', function(req, res, next) {

  res.render('index', { title: TITLE });

});

// 单图上传
router.post('/', upload.array('fulAvatar'), function(req, res, next){

  req.files.forEach(function(ele,index){
    console.log(ele);
    var oldFile=ele.destination+ele.filename;	//指定旧文件

    var date = new Date();
    var upload2=multer({dest:'./upload/'+date.toLocaleDateString()+'/' });
    var newFile=ele.destination+date.toLocaleDateString()+'/'+ele.originalname;	//指定新文件

    console.log(oldFile);
    console.log(newFile);

    fs.rename(oldFile,newFile,function(err){
      err?console.log('上传失败！'):console.log('上传成功！');
    });
  });
  res.locals.success = '上传成功';
  res.render('index', { title: TITLE });

});


module.exports = router;
